import { uploadFileWithFormData } from '@/api';
import AgentServer from '@/modules/Agent';
/** 上传文件到服务器 */
export const uploadFile = async (file: File) => {
	let formData = new FormData();
	formData.append('file', file);
	try {
		let { data } = await uploadFileWithFormData(formData);
		// return { url: data.data.url, size: file.size, name: file.name, suffix: getFileSuffix(file.name) };
        return { url: AgentServer.configure.awsFileUrl ? AgentServer.configure.awsFileUrl + data?.data?.url : data?.data?.url, size: file.size, name: file.name, suffix: getFileSuffix(file.name) };
	} catch (e) {
        console.log('uploadFile error', e);
		return null;
	}
};

/** 获取文件后缀名 */
export const getFileSuffix = (name: string) => {
	return name.substring(name.lastIndexOf('.') + 1);
};
